package com.kdtech.analyse.outside;
import java.util.HashMap;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;

import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * @author KK
 *
 */
public class MinghuiAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://www.minghui.org/.*/[0-9]*/[0-9]*/[0-9]*/.*-[0-9]{5,}.html",
		"http://www.minghui.org/.*/.*/[0-9]*/[0-9]*/[0-9]*/.*-[0-9]{5,}.html",
	};
	
	public static HashMap<String, String> getLinks(String url){
		HashMap<String, String> links = new HashMap<String, String>();
		
		String realUrl = "";
		if(url.matches("http://www.minghui.org/mh/fenlei/298/")){
			realUrl = "http://www.minghui.org/mh/fenlei/298/cat298.js";
			String htmltxt = CrawlHTML.responseToURL(realUrl).getHtml();
			String[] datatext = htmltxt.split("],");
			for(int i = 0; i < datatext.length; i++){
				String data = datatext[i];
				for(int j = 0; j < 5; j++){
					data = StringUtils.substringAfter(data, "\"");
				}
				data = "http://www.minghui.org" + StringUtils.substringBeforeLast(data, "\"");
				if(data.matches("http://www.minghui.org/.*/[0-9]*/[0-9]*/[0-9]*/.*-[0-9]{5,}.html")
						|| data.matches("http://www.minghui.org/.*/.*/[0-9]*/[0-9]*/[0-9]*/.*-[0-9]{5,}.html")){
					links.put(data, "");
				}
			}
		}else if(url.matches("http://www.minghui.org/mh/fenlei/[0-9]*/")){
			String fenleiId = StringUtils.substringBetween(url, "fenlei/", "/");
			realUrl = "http://www.minghui.org/mh/fenlei/"+fenleiId+"/cat"+fenleiId+"-2015.js";
			if(StringUtils.isBlank(url)) return null;
			String htmltxt = CrawlHTML.responseToURL(realUrl).getHtml();
			String jsontext = StringUtils.substringAfter(htmltxt, "=");
			jsontext = StringUtils.substringBeforeLast(jsontext, ";");
			JSONArray jsonArray = JSONObject.parseArray(jsontext);
			for(int i = 0; i < jsonArray.size(); i++){
				try {
					JSONArray dataArray = JSONArray.parseArray(jsonArray.getString(i));
					String detailUrl = "http://www.minghui.org" + dataArray.getString(3);
					links.put(detailUrl, "");
				} catch (Exception e) {
					continue;
				}
			}
		}

		return links;
	}

	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("title").text();
		title =title.replace("【明慧网】", "");
		if(StringUtils.isBlank(title))
			title = doc.select("div.ar_articleTitle").text();
		content=HtmlCleaner.getContentHtml(url,doc.select(".articleContent"));
		if(StringUtils.isBlank(content)) content=HtmlCleaner.getContentHtml(url,doc.select(".jingwenBody"));
		if(StringUtils.isBlank(content)) content=HtmlCleaner.getContentHtml(url,doc.select("div#ar_bArticleContent_OuterFrame"));
		date=JSoupUtils.matchDate(doc, "发稿：","更新：");
		if(date==null){
			date=DateUtils.matchDate(url);
		}
		author=JSoupUtils.matchAuthor(doc, "来源：");
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	

	

	
}
